package com.ibm.news.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ibm.news.beans.Sort;
import java.sql.PreparedStatement;

public class SortDAO {

	public boolean insertSort(Sort sort) {
		Connection conn = DbConn.getConn();
		String sql = "insert into tb_sort (bigSort, smallSort, createTime) values (?, ?, ?)";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, sort.getBigSort());
			ps.setString(2, sort.getSmallSort());
			ps.setString(3, sort.getCreateTime());
			if (ps.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateSort(Sort sort) {
		Connection conn = DbConn.getConn();
		String sql = "update tb_sort set bigSort = ?, smallSort = ?, createTime = ? where id = ?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, sort.getBigSort());
			ps.setString(2, sort.getSmallSort());
			ps.setString(3, sort.getCreateTime());
			ps.setInt(4, sort.getId());
			if (ps.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteSort(int id) {
		Connection conn = DbConn.getConn();
		String sql = "delete from tb_sort where id = ?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			if (ps.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public ArrayList<Sort> getSortAll() {
		Connection conn = DbConn.getConn();
		String sql = "select id, bigSort, smallSort, createTime from tb_sort";
		try {
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery(sql);
			ArrayList<Sort> sortList = new ArrayList<Sort>();
			while (rs.next()) {
				Sort st = new Sort();
				st.setId(rs.getInt("id"));
				st.setBigSort(rs.getString("bigSort"));
				st.setSmallSort(rs.getString("smallSort"));
				st.setCreateTime(rs.getString("createTime"));
				sortList.add(st);
			}
			return sortList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
